package org.gap.wizards.world;

import java.awt.Point;

import org.gap.wizards.Drawable;
import org.gap.wizards.utils.Persistable;
import org.gap.wizards.utils.Persistence;
import org.gap.wizards.views.ViewPlatform;
import org.gap.wizards.views.ViewPlatform.Color;

public class MapTransition implements Drawable, Persistable {
	private static final String OTHER_MAP = "other.map";
	private static final String FROM = "from";
	private static final String DIRECTION = "direction";
	private static final String TO = "to";

	public enum Direction{Up('<'), Down('>'), Over('^');
		private final char dir;
		Direction(char dir) {
			this.dir = dir;
		}
		public char symbol() {
			return dir;
		}
	}

	private Direction direction;
	private Point from;
	private String transitionToMapId;
	private Point to;
	
	public MapTransition(Direction direction, Point from, String transitionToMapId, Point endAt) {
		this.direction = direction;
		this.from = from;
		
		this.transitionToMapId = transitionToMapId;
		this.to = endAt;
	}

	public MapTransition() {
	}

	public boolean isAtPoint(Point location) {
		return from.equals(location);
	}

	@Override
	public void draw(ViewPlatform console) {
		console.print(getX(), getY(), Color.WHITE, direction.symbol());
	}

	@Override
	public int getX() {
		return from.x;
	}

	@Override
	public int getY() {
		return from.y;
	}

	public Direction getDirection() {
		return direction;
	}

	public String getTransitionToMapId() {
		return transitionToMapId;
	}

	public Point getTransitionToLocation() {
		return to;
	}

	@Override
	public void persist(Persistence persistence) {
		persistence.set(DIRECTION, getDirection());
		persistence.set(FROM, from);
		persistence.set(TO, to);
		persistence.set(OTHER_MAP, transitionToMapId);
	}

	@Override
	public void load(Persistence persistence) {
		direction = persistence.getEnum(DIRECTION, Direction.class);
		from = persistence.getPoint(FROM);
		to = persistence.getPoint(TO);
		transitionToMapId = persistence.getString(OTHER_MAP);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj != null && obj.getClass().equals(MapTransition.class)) {
			MapTransition other = (MapTransition)obj;
			return direction == other.direction && 
					from.equals(other.from) &&
					transitionToMapId.equals(other.transitionToMapId) &&
					to.equals(other.to);
		}
		return false;
	}
	
	@Override
	public String toString() {
		return "Direction: " + direction.name() + ", from: " + from + ", to: " + to + ", transition to map: " + transitionToMapId;
	}
}
